function getRootPath() {
	// 获取当前网址，如： http://localhost:8083/uimcardprj/share/meun.jsp
	var curWwwPath = window.document.location.href;
	// 获取主机地址之后的目录，如： uimcardprj/share/meun.jsp
	var pathName = window.document.location.pathname;
	var pos = curWwwPath.indexOf(pathName);
	// 获取主机地址，如： http://localhost:8083
	var localhostPath = curWwwPath.substring(0, pos);
	// 获取带"/"的项目名，如：/uimcardprj
	var projectName = pathName.substring(0,
			pathName.substring(1).indexOf('/') + 1);
	return (localhostPath + projectName);
}
// 绝对定位
function adjustPosition(inputId, treeId) {
	var offsetLeft = $(".main-product-body").offset().left;
	var offsetTop = $(".main-product-body").offset().top;
	var target = $("#" + inputId);
	var targetOffset = $("#" + inputId).offset();
	var totalTop = targetOffset.top + target.outerHeight() - offsetTop
			+ $(".main-product-body").scrollTop();
	$("#" + treeId).css({
		left : (targetOffset.left - offsetLeft) + "px",
		top : totalTop + "px",
		width : target.outerWidth() + "px"
	});
}
// 绝对定位
function handleScrollStateChanged(inputId, treeId) {
	var scrollbarWidthOld = $('.main-product-body')[0].offsetWidth
			- $('.main-product-body')[0].scrollWidth;
	setTimeout(function() {
		var scrollbarWidthNew = $('.main-product-body')[0].offsetWidth
				- $('.main-product-body')[0].scrollWidth;
		if (scrollbarWidthOld != scrollbarWidthNew) {
			var target = $("#" + inputId);
			targetOffset = target.offset();
			offsetLeft = $(".main-product").offset().left;
			$("#" + treeId).css({
				left : (targetOffset.left - offsetLeft) + "px",
				width : target.outerWidth() + "px"
			});
		}
	}, 200);
}
// 初始化下拉列表
function initiateSelector(id, type) {
	$.ajax({
		url : getRootPath() + '/config/getData.do',
		type : "post",
		async : false,
		data : {
			'F_TYPE' : type
		},
		dataType : 'json',
		success : function(data) {
			var list = data.datas;
			var str = "";
			for (var i = 0; i < list.length; i++) {
				str = str + '<OPTION value="' + list[i].F_VALUE + '">'
						+ list[i].F_NAME + '</OPTION>';
			}
			$("#" + id).append(str);
		}
	});
}

/**
 * 初始化下拉框(后台json数据带过来)
 */
function initSel(id,data){
	
	var gzxxInfosel = "";
	var gzxxInfoOptions = eval(data);
	if (gzxxInfoOptions != undefined && gzxxInfoOptions != null && gzxxInfoOptions.length > 0) {
		for ( var i = 0; i < gzxxInfoOptions.length; i++) {
			gzxxInfosel = gzxxInfosel + '<OPTION value="' + gzxxInfoOptions[i].F_VALUE + '">' + gzxxInfoOptions[i].F_NAME + '</OPTION>';
		}
	}
	$("#"+id).append(gzxxInfosel);
}

function resetform(formid) {
	$("#" + formid)[0].reset();
}

// 打开弹出层
/**
 * title 弹出层标题 url 地址 flag 关闭弹出层是否需要刷新页面 width 弹框宽度 height 弹框高度
 */
var diag;
function openDialog(title, url, flag, width, height) {
	top.jzts();// 打开遮罩
	diag = new Dialog();
	diag.Drag = true;
	diag.Title = title;
	diag.URL = getRootPath() + url;
	diag.Width = width;
	diag.Height = height;
	diag.CancelEvent = function() { // 关闭事件
		diag.close();
		if (flag == true || flag == 'true') {
			search();
		}

	};

	diag.show();
}

function getFormatDate(date) // ----- yyyy-mm-dd
{
	var date = new Date(date);

	var seperator1 = "-";

	var seperator2 = ":";

	var year = date.getFullYear();

	var month = date.getMonth() + 1;

	var strDate = date.getDate();

	if (month >= 1 && month <= 9) {
		month = "0" + month;
	}
	if (strDate >= 0 && strDate <= 9) {
		strDate = "0" + strDate;
	}

	var currentdate = year + seperator1 + month + seperator1 + strDate;

	return currentdate;
}
function getFormatDateTime(date) // yyyy-mm-dd hh:mm:ss
{
	var date = new Date(date);

	var seperator1 = "-";

	var seperator2 = ":";

	var year = date.getFullYear();

	var month = date.getMonth() + 1;

	var strDate = date.getDate();

	var hh = date.getHours();

	var mi = date.getMinutes();

	var ss = date.getSeconds();

	if (month >= 1 && month <= 9) {
		month = "0" + month;
	}
	if (strDate >= 0 && strDate <= 9) {
		strDate = "0" + strDate;
	}
	if (hh >= 0 && hh <= 9) {
		hh = "0" + hh;
	}
	if (mi >= 0 && mi <= 9) {
		mi = "0" + mi;
	}
	if (ss >= 0 && ss <= 9) {
		ss = "0" + ss;
	}

	var currentdate = year + seperator1 + month + seperator1 + strDate + " "
			+ hh + seperator2 + mi + seperator2 + ss;

	return currentdate;
}

// 删除单条记录
/**
 * url ：/noticeManage/deleteNotice func : 删除成功回调
 */
function delData(url, data, func) {

	bootbox.dialog("您确定要删除吗!", [ {
		"label" : "确定",
		"class" : "btn-small btn-success",
		"callback" : function(e) {
			top.jzts();

			$.ajax({
				type : "POST",
				url : getRootPath() + url,
				data : data,
				dataType : 'json',
				cache : false,
				success : function(data) {
					top.hangge();
					if (data.msg == 'ok') {
						bootbox.dialog("删除成功!", [ {
							"label" : "关闭",
							"class" : "btn-small btn-success",
							"callback" : func
						} ]);
					} else {
						bootbox.alert("删除失败!");
					}

				},
				error : function() {
					top.hangge();
					bootbox.alert("程序异常");
				}
			});
		}
	}, {
		"label" : "取消",
		"class" : "btn-small btn-success"
	} ])

}

/**
 * 保存
 * 
 * param 传参 msg 添加成功 func 添加成功回调函数
 */
function saveData(url, param, func) {

	top.jzts();
	$.ajax({
		type : "POST",
		url : getRootPath() + url,
		data : param,
		dataType : 'json',
		cache : false,
		success : function(data) {
			top.hangge();
			if (data.msg == 'ok') {
				bootbox.dialog("保存成功!", [ {
					"label" : "关闭",
					"class" : "btn-small btn-success",
					"callback" : func
				} ]);
			} else {
				bootbox.alert("保存失败!");
			}

		},
		error : function() {
			top.hangge();
			bootbox.alert("程序异常");
		}
	});
}

var cb = function(start, end, label) {
	$('#dateRange span').html(start.format('yyyy/MM/dd'));
};

var optionSet1 = {
	// startDate: moment(),
	showDropdowns : false,
	showWeekNumbers : false,
	timePicker : true,
	timePickerIncrement : 1,
	singleDatePicker : true,
	timePicker24Hour : true,
	locale : {
		format : 'yyyy/MM/dd',
		daysOfWeek : [ '日', '一', '二', '三', '四', '五', '六' ],
		monthNames : [ '一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月',
				'十月', '十一月', '十二月' ],
		applyLabel : "应用",
		fromLabel : "开始日期",
		toLabel : "结束日期",
		clearLabel : "取消"
	},
	opens : 'left',
	buttonClasses : [ 'btn btn-default' ],
	applyClass : 'btn-small btn-primary',
	cancelClass : 'btn-small',
	format : 'yyyy/MM/dd'
};

/**
 * 资材分类下拉勾选----start 使用此功能，html部分加入以下代码
 * 初始化入口initZCFLSelector(),即使用此功能的页面，引入这个js方法即可 定义全局变量var zcxlIds = "";
 * queryParams中{zcxlIds:zcxlIds} html中，最外层div中要引入main-product-body这个类，示例如下 <div
 * class="span12 main-product-body" style="width: 100%; margin: 0; padding: 0;">
 * <div id="zcxlDiv" style="z-index: 101; display: none; background-color:
 * white; border: 1px solid; position: absolute; height: 240px;overflow-y: auto;
 * overflow-x: auto;">
 * <ul id="tree" class="tree" style="width: 100%">
 * </ul>
 * </div> <input id="zcxlNames" name="zcxlNames" class="remove-pad-mar"
 * style="margin-top:5px;margin-bottom:0px;width:20%;" readonly type="text"
 * value="" placeholder="请选择设备类型" /> </div>
 * 
 * @returns
 */

function initZCFLSelector() {
	showTreeForZCFL();
	initListenerForZCFL();
}

function showTreeForZCFL() {
	$.ajax({
		url : getRootPath() + '/asset/treeData.do',
		type : "post",
		async : false,
		dataType : 'json',
		success : function(data) {
			var zTreeNodes = eval(data.zTreeNodes);
			var setting = {
				checkable : true,
				callback : {
					change : zTreeOnClickForZCFL
				},
				showLine : true
			};
			zTree = $("#tree").zTree(setting, zTreeNodes);
		}
	});
}

function zTreeOnClickForZCFL(event, treeId, treeNode) {
	var nodes = zTree.getCheckedNodes(true), id = "", v = "";
	for (var i = 0, l = nodes.length; i < l; i++) {
		v += nodes[i].name + ",";
		id += nodes[i].id + ",";
	}

	if (v.length > 0) {
		v = v.substring(0, v.length - 1);
	}
	if (id.length > 0) {
		id = id.substring(0, id.length - 1);
	}
	zcxlIds = id;
	zcxlNames = v;
	$("#zcxlNames").val(zcxlNames);
}
// 下拉菜单
function treeShowMenu() {
	adjustPosition("zcxlNames", "zcxlDiv");
	$("#zcxlDiv").slideDown("fast");
	handleScrollStateChanged("zcxlNames", "zcxlDiv");
	$("body").bind("mousedown", onBodyDown);
}
// 收起菜单
function treeHideMenu() {
	$("#zcxlDiv").fadeOut("fast");
	$("body").unbind("mousedown", onBodyDown);
}
function onBodyDown(event) {
	if (!(event.target.id == "zcxlDiv" || $(event.target).parents("#zcxlDiv").length > 0)) {
		treeHideMenu();
	}
}
function onWindowResize() {
	adjustPosition("zcxlNames", "zcxlDiv");
}

function initListenerForZCFL() {
	// 资产小类
	$("#zcxlNames").bind("click", function() {
		if ($("#zcxlDiv").css("display") == "none") {
			treeShowMenu();
		} else {
			$("#zcxlNames").val(zcxlNames);
			treeHideMenu();
		}
	});
}
